/**
 * 
 */
package top.lcpsky.flashcards.config;

import javax.validation.ConstraintViolationException;
import javax.validation.ValidationException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.NoHandlerFoundException;

import top.lcpsky.flashcards.common.BizException;
import top.lcpsky.flashcards.util.ResponseResult;

/**   
 * @ClassName:  GlobalExceptionHandler   
 * @Description:TODO(这里用一句话描述这个类的作用)   
 * @author: lcp
 * @date:   2020年2月22日 上午9:53:47   
 *     
 * @Copyright: 2020 www.ankept.com Inc. All rights reserved. 
 */
@RestControllerAdvice
public class GlobalExceptionHandler {
	private Logger logger = LoggerFactory.getLogger(getClass());

    private static int DUPLICATE_KEY_CODE = 1001;
    private static int PARAM_FAIL_CODE = 1002;
    private static int VALIDATION_CODE = 1003;
    private static int AUTHC_CODE = 1004;

    /**
     * 处理自定义异常
     */
    @ExceptionHandler(BizException.class)
    public ResponseResult handleRRException(BizException e) {
        logger.error(e.getMessage(), e);
        return new ResponseResult(e.getCode(), e.getMessage());
    }

    /**
     * 方法参数校验
     */
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public ResponseResult handleMethodArgumentNotValidException(MethodArgumentNotValidException e) {
        logger.error(e.getMessage(), e);
        return new ResponseResult(PARAM_FAIL_CODE, e.getBindingResult().getFieldError().getDefaultMessage());
    }

    /**
     * ValidationException
     */
    @ExceptionHandler(ValidationException.class)
    public ResponseResult handleValidationException(ValidationException e) {
        logger.error(e.getMessage(), e);
        return new ResponseResult(VALIDATION_CODE, e.getCause().getMessage());
    }

    /**
     * ConstraintViolationException
     */
    @ExceptionHandler(ConstraintViolationException.class)
    public ResponseResult handleConstraintViolationException(ConstraintViolationException e) {
        logger.error(e.getMessage(), e);
        return new ResponseResult(PARAM_FAIL_CODE, e.getMessage());
    }

    @ExceptionHandler(NoHandlerFoundException.class)
    public ResponseResult handlerNoFoundException(Exception e) {
        logger.error(e.getMessage(), e);
        return new ResponseResult(404, "路径不存在，请检查路径是否正确");
    }

    @ExceptionHandler(DuplicateKeyException.class)
    public ResponseResult handleDuplicateKeyException(DuplicateKeyException e) {
        logger.error(e.getMessage(), e);
        return new ResponseResult(DUPLICATE_KEY_CODE, "数据重复，请检查后提交");
    }


    @ExceptionHandler(Exception.class)
    public ResponseResult handleException(Exception e) {
        logger.error(e.getMessage(), e);
        return new ResponseResult(500, "系统繁忙,请稍后再试");
    }
    
}
